package cn.shentianlan.controller;

import cn.shentianlan.domain.Device;
import cn.shentianlan.domain.DeviceTable;
import cn.shentianlan.service.DeviceService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
public class DeviceController {
    @Resource
    private DeviceService deviceService;
    /**
     * 表单数据显示
     * @param request
     * @return
     */
    @RequestMapping(value = "/showDevice.do")
    public void findDevice(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int start = Integer.parseInt(request.getParameter("start"));//开始页数
        int length = Integer.parseInt(request.getParameter("length"));//查询数据的长度
        int draw = Integer.parseInt(request.getParameter("draw"));//查询次数
        String searchValue = request.getParameter("search.value");//要搜索数据的值
        DeviceTable deviceTable = deviceService.showDevice(start,length,draw,searchValue);
        //将deviceTable转成json数据
        ObjectMapper mapper = new ObjectMapper();
        String jsonString = mapper.writeValueAsString(deviceTable);
        response.getWriter().write(jsonString);
    }

    /**
     * 获取所有设备信息
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "getDevice.do")
    public void getDeviceId(HttpServletResponse response) throws IOException {
        List<Device> list = deviceService.getDevice();
        ObjectMapper mapper = new ObjectMapper();
        String jsonString = mapper.writeValueAsString(list);
        response.setHeader("Content-Type", "text/html;charset=UTF-8");
        response.getWriter().write(jsonString);
    }

    /**
     * 根据设备id删除设备
     * @param gd_dev_id
     */
    @RequestMapping(value = "/deleteDevice.do",method= RequestMethod.POST)
    public void deleteDevice(@RequestParam("gd_dev_id") String gd_dev_id, HttpServletResponse response) throws IOException {
        boolean b = deviceService.deleteDevice(gd_dev_id);
        response.getWriter().print(b);
    }

    /**
     * 添加设备
     */
    @RequestMapping(value = "/addDevice.do",method = RequestMethod.POST)
    public String addDevice(Device device, HttpServletResponse response) throws IOException {
        try{
            deviceService.addDevice(device);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "/WEB-INF/pages/tables/deviceManage.jsp";
    }

    @RequestMapping(value = "/updateDevice.do",method = RequestMethod.POST)
    public String updateDevice(Device device, HttpServletResponse response) throws IOException {
        deviceService.updateDevice(device);
        return "/WEB-INF/pages/tables/deviceManage.jsp";
    }

}
